<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/8 0008
 * Time: 15:49
 */

namespace app\common\controller;

class zsPayController
{
    /**
     * 浙商银行签名
     * @param $srcdata
     * @param $signdata
     * @return int
     */
    public function CZBSign($srcdata, &$signdata)
    {
        $tmpfname = tempnam("", "CZ");
        $handle = fopen($tmpfname, "w");
        fwrite($handle, $srcdata);
        fclose($handle);

        $tmpsignname = tempnam("", "SS");

        if (openssl_pkcs7_sign($tmpfname, $tmpsignname, "file://d:/phpStudy/PHPTutorial/WWW/oumaos/public/mall2.pem",
            array("file://d:/phpStudy/PHPTutorial/WWW/oumaos/public/mall2.pem", "a111111"),
            array(""),PKCS7_BINARY|PKCS7_NOCHAIN|PKCS7_NOATTR
        ))
        {
            $data = file_get_contents($tmpsignname);
            $parts = explode("\n\n", $data, 2);
            $signdata  = str_replace("\n", "", $parts[1]);
            unlink($tmpfname);
            unlink($tmpsignname);
            return 1;
        }
        unlink($tmpfname);
        return 0;
    }

    /**
     * 浙商银行发起支付
     */
    public function pay(){
        global $signMsg2, $token;


//        支付流水号	tradeNo	char(30)	R	每次支付需使用不同的支付流水号
//        支付金额	payAmount	decimal(15,2)	R	实际需要支付金额
//        支付设备	payDevice	char(1)	R	0 android，1 ios
//        备注	remark	char(100)	O
//        签名	sign	char(-)	R	字符串签名结果（method+version+merId+date+tradeNo+payAmount+payDevice+remark）


        $method = "mobilePay";
        $version = '1_0';
        $merId = '88888888';    //商户号
        $data = date("YmdHis",time());;     //交易发起的日期时间，格式yyyyMMddHHmmss
        $tradeNo = 'PO12345678910';  //账单流水号
        $payAmount = '0.01';//实际需要支付金额
        $payDevice = '0';//0 android，1 ios
        $remark = '测试';   //

        //字符串签名结果（method+version+merId+date+tradeNo+payAmount+payDevice+remark）
        $sign = $method.$version.$merId.$data.$tradeNo.$payAmount.$payDevice.$remark;
//        $signMsg = '';//随机数
//
//        $this->CZBSign($sign, $signMsg);

        $postData = array (
            'method'   =>  $method,
            'version'=>  $version,
            'merId'           =>  $merId,
            'data'       =>  $data,
            'tradeNo'         =>  $tradeNo,
            'payAmount'         =>  $payAmount,
            'payDevice'         =>  $payDevice,
            'remark'         =>  $remark,
            'sign'         =>  $sign,
//            'tradeNo'         =>  $signMsg,
        );

        $postData = http_build_query($postData);

        //print $data;
        $opts = array (
            'http' => array (
                'method' => 'POST',
                'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
                    . "Content-Length: " . strlen($postData) . "\r\n",
                'content' => $postData
            )
        );
//        https://115.238.51.98:465/B2BPAY/getToken
        $context = stream_context_create($opts);
        $httpresp = file_get_contents('http://60.191.15.90:29082/B2BPAY/', false, $context);
        $httpresp = iconv("GBK", "UTF-8", $httpresp);
        $res = json_decode($httpresp,true);
        dump($postData);
        die;

        $token = $res['token'];
        if($res['errno'] == '000000'){
            $signraw = 'CZB_B2B_ORDER_COMMIT1_0'.$token;
            CZBSign($signraw, $signMsg2);
        }else{
            $note = $res['errmsg'];
        }
    }
}